package com.brightfuture.chrms.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brightfuture.chrms.common.core.util.R;
import com.brightfuture.chrms.common.log.annotation.SysLog;
import com.brightfuture.chrms.entity.PersonEntity;
import com.brightfuture.chrms.entity.PersonPositionEntity;
import com.brightfuture.chrms.service.PersonService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.brightfuture.chrms.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 人员;人员表
 *
 * @author Victor
 * @date 2024-02-03 17:00:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/person" )
@Tag(description = "person" , name = "人员;人员表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PersonController {

    private final  PersonService personService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param person 人员;人员表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('chrms_person_view')" )
    public R getPersonPage(@ParameterObject Page page, @ParameterObject PersonEntity person) {
        LambdaQueryWrapper<PersonEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(personService.page(page, wrapper));
    }

    /**
     * 通过id查询人员;人员表
     * @param perId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{perId}" )
    @PreAuthorize("@pms.hasPermission('chrms_person_view')" )
    public R getById(@PathVariable("perId" ) Long perId) {
        return R.ok(personService.getByIdDeep(perId));
    }

    /**
     * 新增人员;人员表
     * @param person 人员;人员表
     * @return R
     */
    @Operation(summary = "新增人员;人员表" , description = "新增人员;人员表" )
    @SysLog("新增人员;人员表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('chrms_person_add')" )
    public R save(@RequestBody PersonEntity person) {
        return R.ok(personService.saveDeep(person));
    }

    /**
     * 修改人员;人员表
     * @param person 人员;人员表
     * @return R
     */
    @Operation(summary = "修改人员;人员表" , description = "修改人员;人员表" )
    @SysLog("修改人员;人员表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('chrms_person_edit')" )
    public R updateById(@RequestBody PersonEntity person) {
        return R.ok(personService.updateDeep(person));
    }

    /**
     * 通过id删除人员;人员表
     * @param ids perId列表
     * @return R
     */
    @Operation(summary = "通过id删除人员;人员表" , description = "通过id删除人员;人员表" )
    @SysLog("通过id删除人员;人员表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('chrms_person_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(personService.removeDeep(ids));
    }

    /**
     * 通过id删除人员;人员表子表数据
     * @param ids perId列表
     * @return R
     */
    @Operation(summary = "通过id删除人员;人员表子表数据" , description = "通过id删除人员;人员表子表数据" )
    @SysLog("通过id删除人员;人员表子表数据" )
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('chrms_person_del')" )
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(personService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     * @param person 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('chrms_person_export')" )
    public List<PersonEntity> export(PersonEntity person,Long[] ids) {
        return personService.list(Wrappers.lambdaQuery(person).in(ArrayUtil.isNotEmpty(ids), PersonEntity::getPerId, ids));
    }
}